{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.yiibai.com/scipy/scipy_stats.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# scipy.stats.norm\n",
    "一个正常的连续随机变量。  \n",
    "所有的统计函数都位于`scipy.stats`中，并且可以使用`info(stats)`函数获取这些函数的完整列表。随机变量列表也可以从`stats`子包的`docstring`中获得。该模块包含大量的概率分布以及不断增长的统计函数库。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 正太连续随机变量\n",
    "随机变量X可以取任何值的概率分布是连续的随机变量。位置(loc) 关键字指定平均值。比例（scale）关键字指定标准偏差。 \n",
    "\n",
    "作为rv_continuous类的一个实例，norm对象从它继承了一组泛型方法，and completes them with details specific for this particular distrubution.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要计算多个点的CDF（累计分布函数），可以传递一个列表或一个Numpy数组。示例如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.84134475, 0.15865525, 0.5       , 0.84134475, 0.9986501 ,\n",
       "       0.99996833, 0.02275013, 1.        ])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy.stats import norm\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "cdf_arr = norm.cdf(np.array([1,-1.,0,1,3,4,-2,6]))\n",
    "cdf_arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要查找分布的中位数，可以使用百分点函数（PPF），它是CDF的倒数。如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ppf_var = norm.ppf(0.5)\n",
    "ppf_var"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要生成随机变量序列（RVS），应该使用`size`参数，如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.12033857, -0.22499568, -0.35621465, -1.04870541,  1.22976163])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rvs_arr = norm.rvs(size=5) \n",
    "rvs_arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意：**  \n",
    "上述输出不可重复。要生成相同的随机数，请使用seed()函数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 均匀分布\n",
    "使用uniform()函数可以生成均匀分布。如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.  , 0.  , 0.25, 0.5 , 0.75, 1.  ])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy.stats import uniform\n",
    "\n",
    "cvar = uniform.cdf([0,1,2,3,4,5], loc=1, scale=4)\n",
    "cvar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# stats.norm.rvs\n",
    "rvs(*args, **kwds) methods of `scipy.stats._continuous_distns.norm_gen` instance    \n",
    "Random variates of given type."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Patameters**  \n",
    "- arg1,arg2,arg3,……: array_like\n",
    "    The shape parameter(s) for the distribution (see docstring of the instance object for more information).  \n",
    "- size: int or tuple of ints, optional  \n",
    "Defining number of random variates (default is 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# stats.norm.fit\n",
    "fit(data, **kwds) method of `scipy.stats._continuous_distns.norm_gen` instance  \n",
    "Return MLEs(极大似然估计) for shape (if applicable), location, and scale parameters from data.  \n",
    "\n",
    "MLE stands for `Maximun Likelihood Estimate`.  Starting estimates for the fit are given by input arguments; for any arguments not provided with starting estimates, `self._fitstart(data)` is called to generate such.  \n",
    "\n",
    "One can hold some parameters fixed to specific values by passing in keyward arguments `f0`,`f1`,....`fn` (for shape parameters) and `floc` and `fscale` (for location and scale parameters, respectively).  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Parameters**   \n",
    "- data: array_like  \n",
    "Data to use in calculating the MLEs.  \n",
    "- args: floats, optional  \n",
    "Starting value(s) for any shape-characterizing(形状特征) arguments (those not provided will be determined by a call to `_fitstart(data)`)  \n",
    "No default value.\n",
    "- kwds: floats, optional  \n",
    "Starting values for the location and scale parameters; no default.  \n",
    "Specific keyward arguments are recognized as holding certain parameters fixed:  \n",
    "1、f0...fn: hold respective shape parameters fixed.  \n",
    "  Alternatively, shape parameters to fix can be specified by name.  \n",
    "  For example, `self.shapes==a, b`, `fa` and `fix_a` are equivalent to `f0`, and `fb` and `fix_b` are equivalent to `f1`.  \n",
    "  2、floc: hold location parameter fixed to specified value.  \n",
    "  3、fscale: hold scale parameter fixed to specified value.  \n",
    "  4、optimizer: The optimizer to use.  The optimizer must take `func`, and starting     position as the first two arguments, plus `args` (for extra arguments to pass     to the function to be optimized) and `disp=0` to suppress output as keyward       arguments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Examples**  \n",
    "Generate some data to fit: draw random variates from the `beta` distribution  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import beta\n",
    "\n",
    "a, b = 1, 2\n",
    "x = beta.rvs(a, b, size=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now we can fit all four parameters (a, b, loc, scale)\n",
    "a1, b1, loc1, scale1 = beta.fit(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.961762344378715"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6940958494159537"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.00024155413182433145"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loc1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.040144823217864"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scale1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We can also use some prior knowledge about the dataset: let's keep 'loc' and 'scale' fixed:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 1)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1, b1, loc1, scale1 = beta.fit(x, floc=0, fscale=1)\n",
    "loc1, scale1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
